package com.example.newproject_overseas.util
import CryptUtils.encryptAes
import android.content.Context
import com.google.android.gms.ads.identifier.AdvertisingIdClient
import com.google.android.gms.common.ConnectionResult
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import android.provider.Settings
import com.example.newproject_overseas.MyApplication

//获得GAID
fun getGoogleAdvertisingId(context: Context, callback: (String) -> Unit) {
    // Get the GAID using a background thread or coroutine
    GlobalScope.launch(Dispatchers.IO) {
        try {
            val adInfo = AdvertisingIdClient.getAdvertisingIdInfo(context)
            val gaid = adInfo?.id ?: ""
            callback(gaid)
        } catch (e: Exception) {
            // Handle the exception, if any
            e.printStackTrace()
        }
    }
}

//获得android  id
fun getAndroidId(context: Context): String {
    return Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
}

//获得token
/**
 * gaid google框架手机才有的一个唯一标识
 * aid  android 唯一标识
 */
fun getToken(gaid:String , aid:String):String{

    val version = "1.0.0" // 我们内部定义的版本好
    val rv = "1.0.0"      //app的版本号
    val tokenData = "{\"GAID\":\"$gaid\",\"VERSION\":\"$version\",\"RV\":\"$rv\",\"AID\":$aid}"

    val appId = "8RACTAZO" // appId
    val secretKey = "$appId${"Aes"}$tokenData" // 密钥key
    val token = "$appId+Aes(${encryptAes(tokenData, secretKey)})"
    print(token)
    return token
}

